//
//  NSString+HXString.m
//  HXTClient
//
//  Created by lemon on 14-4-4.
//  Copyright (c) 2014年 easaa. All rights reserved.
//

#import "NSString+HXString.h"

@implementation NSString (HXString)

- (CGSize)sizeWithFont:(UIFont *)font andConstrainedToSize:(CGSize)size
{
    CGSize selfSize = CGSizeZero;
    if (HXOSVersion() >= 7.0f)
    {
        NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil];
        CGRect rect = [self boundingRectWithSize:size options:NSStringDrawingTruncatesLastVisibleLine |
                                                                NSStringDrawingUsesLineFragmentOrigin
                                      attributes:attributesDictionary context:nil];
        selfSize = rect.size;
    } else
    {
        selfSize = [self sizeWithFont:font constrainedToSize:size lineBreakMode:NSLineBreakByWordWrapping];
    }
    return selfSize;
}

- (NSString *)dateStringWithFormatter:(NSString *)oneFormatter convertToFormatter:(NSString *)twoFormatter
{
    NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:oneFormatter];
    NSDate *date = [dateformatter dateFromString:self];
    
    [dateformatter setDateFormat:twoFormatter];
    return [dateformatter stringFromDate:date];
}

- (NSDate *)stringDateConvertToDateWithFormatter:(NSString *)formatter
{
    NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:formatter];
    return [dateformatter dateFromString:self];
}

- (BOOL)validatePhone
{
    NSString *phoneRegex = @"^((13[0-9])|(147)|(15[^4,\\D])|(18[0-9]))\\d{8}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex];
    return [phoneTest evaluateWithObject:self];
}

+ (NSString *)dateStirng:(NSString *)dateString
        withOldFormatter:(NSString *)oldFormatter
   convertToNewFormatter:(NSString *)newFormatter
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:oldFormatter];
    NSDate *date = [dateFormatter dateFromString:dateString];
    [dateFormatter setDateFormat:newFormatter];
    NSString *str = [dateFormatter stringFromDate:date];
    return str;
}

+ (NSString *)DateStringConvertToCommentTime:(NSString *)dateString formatter:(NSString *)formatter
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:formatter];
    NSDate *date = [dateFormatter dateFromString:dateString];
    
    NSDate *curDate = [NSDate date];
    NSTimeInterval interval = [curDate timeIntervalSinceDate:date];
    
    NSString *tipString = @"未知";
    
    NSTimeInterval hours = interval/60/60; //多少小时
    
    NSUInteger days = hours/24;
    NSUInteger weeks = days/7;
    NSUInteger months = days/30;
    NSUInteger years = days/365;
    
    if (days < 7)
    {
        tipString = [NSString stringWithFormat:@"%d天前", days];
    } else if (weeks <= 4)
    {
        tipString = [NSString stringWithFormat:@"%d周前", weeks];
    } else if (months < 12)
    {
        tipString = [NSString stringWithFormat:@"%d月前", months];
    } else if (years >= 1)
    {
        tipString = [NSString stringWithFormat:@"%d年前", years];
    }
    
    return tipString;
}

@end
